package com.totoro.tymapping.executor.slotparse;

import com.totoro.tymapping.constants.ConditionConst;
import com.totoro.tymapping.enums.ConditionExecutorEnum;
import com.totoro.tymapping.executor.condition.ConditionExecutor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Totoro
 * @version 0.1.0
 * @date 2025/8/13 - 15:19
 * @description 槽位解析执行器，槽位只解析name和lore的条件
 **/
public interface SlotParseExecutor {

    /**
     * @author totoro
     * @description 获取最终槽位，也就是Minecraft#1 这里的1，或者DragonCore#项链  这里的项链
     * @date  15:56
     * @param slot
     * @return java.lang.String
     **/
    default String getFinalSlot(String slot) {
        return slot.split("#")[1];
    }

    /**
     * @author totoro
     * @description 获取最终的条件，对于槽位的条件解析那么条件只有name和lore
     * @date  15:57
     * @param condition
     * @return java.util.List<java.lang.String>
     **/
    default List<String> getSlotCondition(List<String> condition) {
        return condition.stream().filter(str -> str.startsWith(ConditionConst.NAME) || str.startsWith(ConditionConst.LORE)).collect(Collectors.toList());
    }

    /**
     * @author totoro
     * @description 获取最终的解析结果，需要ItemStack
     * @date  15:57
     * @param condition, itemStack, player
     * @return java.lang.Boolean
     **/
    default Boolean doParseValue(List<String> condition, ItemStack itemStack, Player player) {
        if(itemStack == null || !itemStack.hasItemMeta() || itemStack.getItemMeta() == null) {
            return false;
        }
        for (String cd : condition) {
            ConditionExecutor conditionExecutor = ConditionExecutorEnum.getConditionExecutor(cd);
            if(conditionExecutor == null) {
                return false;
            }
            return conditionExecutor.execute(cd, player, itemStack);
        }
        return true;
    }

    /**
     * @author totoro
     * @description 执行解析
     * @date  15:57
     * @param slot, condition, player
     * @return java.lang.Boolean
     **/
    Boolean execute(String slot, List<String> condition, Player player);
}
